(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.fxRate.createRejectController', createRejectController);
	createRejectController.$inject = [ '$scope', '$q', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'fxRateService' ];
	function createRejectController($scope, $q, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, fxRateService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.fxRate = {};
		ctrl.fxRateHistoryLookup = function(){
			fxRateService.fxRateHistoryLookup(ctrl.selectedRows);
		};
		ctrl.init = function() {
			if(ctrl.validatorConfig) ctrl.layoutConfig.formConfigs.general.tableConfigs.table.validatorConfig = ctrl.validatorConfig.fxRateList;
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.general.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.fxRate.fxRateList';
			var defer1 = $q.defer();
			var defer2 = $q.defer();
			fxRateService.initOptions().then(function(result){
				ctrl.currencyOptions = result.currencyOptions;
				ctrl.baseCcy = result.baseCcy;
				defer1.resolve();
			});
			fxRateService.getSiteInfor().then(function(response) {
				ctrl.bizDate = response.bizDate;
				defer2.resolve();
			}, angular.noop);
			($q.all({
				defer1 : defer1.promise,
				defer2 : defer2.promise
			})).then(function(){
				authService.createReject(params).then(function(response) {
					if (response.data && response.data.model && response.data.task) {
						ctrl.fxRate = response.data.model || {};
						ctrl.fxRate.taskId = response.data.task.taskId;
						ctrl.fxRate.fxRateList = ctrl.fxRate.fxRateList || [];
					}
				});
			});
		};
		
		ctrl.addFxRate = function(row) {
			return $q(function(resoved, rejected){
				row.ccyTo = ctrl.baseCcy;
				row.effectiveDate = ctrl.bizDate;
				resoved();
			});
		};

		ctrl.createRejectSave = function() {
			return authService.createRejectSave(ctrl.fxRate).then(saveSuccessful, saveFailed);
		};

		ctrl.createRejectSubmit = function() {
			return authService.createRejectSubmit(ctrl.fxRate).then(submitSuccessful, submitFailed);
		};
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}
		ctrl.createRejectCancel = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.cancel', args : [ctrl.fxRate.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createRejectCancel(ctrl.fxRate).then(cancelSuccessful, cancelFailed);
					}
				}
			});
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.fxrate.list',
					'state' : 'cda.fxRate.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.fxRate = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function cancelSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.cancel' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.fxRate.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.fxrate.list',
					'state' : 'cda.fxRate.list',
					'params' : {}
				}]
			});
		}

		function cancelFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);